package com.shi.controller;

import com.shi.annotation.auth.MyAuth;
import com.shi.annotation.cache.MyCacheEvict;
import com.shi.annotation.cache.MyCacheable;
import com.shi.annotation.limit.RateLimitRule;
import com.shi.annotation.limit.RateLimiter;
import com.shi.annotation.log.MyLog;
import com.shi.common.Result;
import com.shi.entity.po.Emp1;
import com.shi.enums.LimitType;
import com.shi.enums.LogCodeEnum;
import com.shi.enums.PermCodeEnum;
import com.shi.service.Emp1Service;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Emp1)表控制层
 *
 * @author makejava
 * @since 2024-05-18 10:47:56
 */
@RestController
@RequestMapping("/emp1")
public class Emp1Controller {
    /**
     * 服务对象
     */
    @Resource
    private Emp1Service emp1Service;

    /**
     * 对于接口，1分钟可以接收1000个请求
     * 对于一个ip，1分钟可以接收10个请求
     *
     * @return
     */
    @RateLimiter(rules = {@RateLimitRule(time = 60, count = 1000)})
    @RateLimiter(rules = {@RateLimitRule(time = 60, count = 10)}, type = LimitType.IP)
    @GetMapping("/test")
    public Result test() {
        return Result.ok("hello");
    }

    /**
     * 通过主键查询单条数据
     *
     * @return 单条数据
     */
    @MyLog(type = LogCodeEnum.SELECT)
    @MyCacheable(key = "'emp1:all'")
    @GetMapping("/all")
    public Result queryAll() {
        return Result.ok(this.emp1Service.queryAll());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @MyCacheable(key = "'emp1:id:'+#id")
    public Result queryById(@PathVariable("id") Integer id) {
        return Result.ok(this.emp1Service.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param emp1 实体
     * @return 新增结果
     */
    @MyCacheEvict(likeKeys = {"'emp1:all'", "'emp1:id:'+#emp1.id"})
    @MyLog(type = LogCodeEnum.INSERT)
    @PostMapping
    public Result add(@RequestBody Emp1 emp1) {
        return Result.ok(this.emp1Service.add(emp1));
    }

    /**
     * 批量新增数据
     *
     * @param emp1s 实体
     * @return 新增结果
     */
    @PostMapping("/batch")
    public Result addBatch(@RequestBody List<Emp1> emp1s) {
        return Result.ok(this.emp1Service.addBatch(emp1s));
    }

    /**
     * 编辑数据
     *
     * @param emp1 实体
     * @return 编辑结果
     */
    @PutMapping
    public Result edit(@RequestBody Emp1 emp1) {
        return Result.ok(this.emp1Service.updateById(emp1));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @MyLog(type = LogCodeEnum.DELETE, desc = "'通过【id='+#id+'】删除'")
    @MyAuth(hasPerm = PermCodeEnum.EMP_DELETE)
    @DeleteMapping("/{id}")
    @MyCacheEvict(likeKeys = {"'emp1:all'", "'emp1:id:'+#id"})
    public Result removeById(@PathVariable("id") Integer id) {
        return Result.ok(this.emp1Service.removeById(id));
    }

    /**
     * 批量删除数据
     *
     * @param ids 主键列表
     * @return 删除是否成功
     */
    @DeleteMapping("/batch")
    public Result removeBatchByIds(List<Integer> ids) {
        return Result.ok(this.emp1Service.removeBatchByIds(ids));
    }

}

